<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>老师主页</title>
    <link rel="stylesheet" href="layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label">序号：</label>
        <div class="layui-input-inline">
            <input type="text" name="hid" id="hidReload" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">编号：</label>
        <div class="layui-input-inline">
            <input type="text" name="tid" id="tidReload" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">账号：</label>
        <div class="layui-input-inline">
            <input type="text" name="tusername" id="tusernameReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">姓名：</label>
        <div class="layui-input-inline">
            <input type="text" name="tname"  id="tnameReload" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">体温范围：</label>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text" name="temperature" id="temperatureReload" placeholder="℃" class="layui-input">
        </div>
        <div class="layui-form-mid">-</div>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text" name="maxTemperature" id="maxTemperatureReload" placeholder="℃" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">打卡地址：</label>
        <div class="layui-input-inline">
            <input type="text" name="area"  id="areaReload" class="layui-input">
        </div>
    </div>

    <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
        <label class="layui-form-label">打卡日期：</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" name="date" id="dateReload">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-inline">
            <button type="submit" class="layui-btn" data-type="reload" id="search">搜索</button>
            <button class="layui-btn"  type="reset" id="reset">重置</button>
        </div>
    </div>
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addTeacherHealth">增加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script th:src="@{layui/layui.js}" charset="utf-8"></script>
<script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use(['table','form','laydate'], function(){
        var table = layui.table;
        var form  = layui.form;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#dateReload' //指定元素
        });

        //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
        //详见：https://www.layui.com/doc/modules/table.html#totalRow
        table.render({
            elem: '#test'  //绑定元素
            ,url:'/teacherHealth'  //数据来源
            ,toolbar: '#toolbarDemo'   //映射绑定
            ,title: '老师数据表'   //导出数据所使用的的名单
            ,totalRow: true
            ,limits:[10,20,40,80,30000]
            ,cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'hid', title:'序号', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                    ,{field:'tid', title:'编号', width:80, edit: 'text', sort: true}
                    ,{field:'tusername', title:'工号(账号)', width:130, edit: 'text', sort: true}
                    ,{field:'tname', title:'姓名', width:80}
                    ,{field:'temperature', title:'温度', width:80, edit: 'text', sort: true,
                            templet:function(res){
                                if(res.temperature>=37.3){
                                    return '<a class="layui-btn layui-btn-danger layui-btn-xs">'+res.temperature+'</a>';
                                }else{
                                    return '<a class="layui-btn layui-btn-xs">'+res.temperature+'</a>';
                                }
                            }
                        }
                    ,{field:'date', title:'日期', width:200, sort: true}
                    ,{field:'area', title:'打卡地区'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]
            ]
            ,page: true //分页
            ,id:'teacherHealthTable'
        });

        var $ = layui.$,active = {
            reload:function(){
                var hidReload = $('#hidReload');
                var tidReload = $('#tidReload');
                var tusernameReload = $('#tusernameReload');
                var tnameReload = $('#tnameReload');
                var temperatureReload = $('#temperatureReload');
                var maxTemperatureReload = $('#maxTemperatureReload');
                var dateReload = $('#dateReload');
                var areaReload = $('#areaReload');


                var index = layer.msg("查询中，请稍后...",{
                    icon:16,
                    time:false,
                    shade:0
                });
                setTimeout(function(){
                    table.reload('teacherHealthTable',{
                        page:{
                            curr:1
                        }
                        ,where:{
                            /*key:{*/
                            hid:hidReload.val(),
                            tid:tidReload.val(),
                            tusername:tusernameReload.val(),
                            tname:tnameReload.val(),
                            temperature:temperatureReload.val(),
                            maxTemperature:maxTemperatureReload.val(),
                            date:dateReload.val(),
                            area:areaReload.val(),

                            /*}*/
                        },
                        method:'POST',
                        url:'teacherHealth/searchTeacherHealth'
                    });
                    layer.close(index);
                },800);

            }
        };

        $('#search').on('click',function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this):'';
        })

        $('#reset').on('click',function(){
            $('#hidReload').val('');
            $('#tidReload').val('');
            $('#tusernameReload').val('');
            $('#tnameReload').val('');
            $('#temperatureReload').val('');
            $('#maxTemperatureReload').val('');
            $('#dateReload').val('');
            $('#areaReload').val('');
        });


        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){   //当前点击
                /*case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;*/
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'addTeacherHealth':
                    var index = layer.open({
                        title: '添加用户',
                        type: 2, //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        shade:0.2,
                        maxmin: true,
                        shadeClose : true,
                        area :['80%','80%'],
                        content: '/addTeacherHealth',
                    });
                    $(window).on("resize",function(){
                        layer.full(index)
                    });
                    return false;
            };
        });

        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //直接向服务器发送删除请求
                    $.ajax({
                        url:'teacherHealth/deleteTeacherHealthById',
                        type:'POST',
                        data:{hid:data.hid},
                        success: function(result){
                            if(result.code===200){
                                layer.close(index);
                                layer.msg("删除成功！");
                                window.location.reload();
                            }else{
                                layer.msg("删除失败！");
                            }
                        }
                    })
                });
            } else if(obj.event === 'edit'){
                layer.msg("修改"+data.sid);
                //打开弹出层
                var index = layer.open({
                    type: 1,
                    title: "修改学生健康信息",
                    shade:0.2,
                    maxmin: true,
                    shadeClose : true,
                    area :['80%','80%'],
                    content: $("#teacherHealthUpdateTest")
                });
                $(window).on("resize",function(){
                    layer.full(index)
                });
                //方式1通过Id查询该记录，方式2，获取该选中记录，然后data渲染。
                setFormValue(obj,data);
            }
        });

        function setFormValue(obj,data){
            console.log(data);

            form.val("updateBtn",{

                "hid":data.hid,
                "tid":data.tid,
                "tusername":data.tusername,
                "tname":data.tname,
                "temperature":data.temperature,
                "date":data.date,
                "area":data.area,

            });
            form.on('submit(updateBtn)',function(data){
                var datas = data.field;
                console.log(datas);

                //向后端发送数据并进行添加操作
                $.ajax({
                    url:"teacherHealth/updateTeacherHealthSubmit",
                    type: "POST",
                    data:datas,
                    success:function(result){
                        //将json对象转成string
                        // result = JSON.parse(result);
                        if(result.code===200){
                            layer.msg("修改成功！",{
                                icon:6,
                                time:500
                            },function(){
                                window.location.reload();//重新加载页面
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                        }else{
                            layer.msg("修改失败！");
                        }
                    }
                })

            })
        };


    });
</script>

<div class="layui-form layuimini-form" id="teacherHealthUpdateTest" style="display:none" lay-filter="updateBtn">
    <input type="hidden" name="hid" />
    <div class="layui-form-item">
        <label class="layui-form-label required">学生编号</label>
        <div class="layui-input-block">
            <input type="text" name="tid" disabled="disabled" class="layui-input">
            <!--<tip>填写自己管理账号</tip>-->
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">学生账号(学号)</label>
        <div class="layui-input-block">
            <input type="text" name="tusername" disabled="disabled" class="layui-input">
            <!--<tip>填写自己管理账号</tip>-->
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="tname" disabled="disabled" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">体温</label>
        <div class="layui-input-block">
            <input type="number" name="temperature" lay-verify="required" lay-reqtext="体温不能为空" placeholder="请输入体温(数字)" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item"> <!-- 注意：这一层元素并不是必须的 -->
        <label class="layui-form-label required">打卡日期</label>
        <div class="layui-input-block">
            <input type="text" name="date" lay-verify="required" lay-reqtext="打卡日期不能为空" class="layui-input" id="dateSelect1">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">打卡地区</label>
        <div class="layui-input-block">
            <input type="text" name="area" lay-verify="required" lay-reqtext="打卡地区不能为空" placeholder="请输入打卡地区" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="updateBtn">确认修改</button>
        </div>
    </div>
</div>


</body>
</html>